/**
 * uniBoard Common JS functions
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-20 21:23:27 +0000 (Mon, 20 Sep 2010) $
 * $Revision: 64 $
 * $Author: rafio.xudb@gmail.com $
 */
 
$( document).ready( function(){
	$('.delete_confirm').click( function(){
		var confirm_result = confirm( l_delete_item_sure);
		return confirm_result;
	});
	$('.delete_items_confirm').click( function(){
		var confirm_result = confirm( l_delete_selected_items_sure);
		return confirm_result;
	});
});

function storeCaret(ftext){
	
	if (ftext.createTextRange){
		ftext.caretPos = document.selection.createRange().duplicate();
	}
	
}

function UniRange(start, end) {
	this.start = start;
	this.end = end;
}

function getSelection(textId) {
	ctrl = document.getElementById(textId);
	if (document.selection) {
		ctrl.focus ();
		var range = document.selection.createRange ();
		var length = range.text.length;
		range.moveStart('character', -ctrl.value.length);
		return new UniRange(range.text.length - length, range.text.length);
	} else if (ctrl.selectionStart || ctrl.selectionStart == '0') {
		return new UniRange(ctrl.selectionStart, ctrl.selectionEnd);
	}
}

function setSelection(textId, UniRange) {
	ctrl = document.getElementById(textId);
	if (ctrl.setSelectionRange) {
		ctrl.focus();
		ctrl.setSelectionRange(UniRange.start, UniRange.end);
	} else if (ctrl.createTextRange) {
		var range = ctrl.createTextRange();
		range.collapse(true);
		range.moveStart('character', UniRange.start);
		range.moveEnd('character', UniRange.end);
		range.select();
	}
}

function makeDoubleTag(textId, tag, parameter) {
	var myRange = getSelection( textId);
	var ctrl = document.getElementById(textId);
	var text = ctrl.value;
	var param = new String(parameter);
	if( param != 'undefined'){
		var startText = text.substring(0, myRange.start) + "[" + tag + "=\"" + param + "\"]";
	}else{
		var startText = text.substring(0, myRange.start) + "[" + tag + "]";
	}
	var middleText = text.substring(myRange.start, myRange.end);
	var endText =  "[/" + tag + "]" + text.substring(myRange.end);
	ctrl.value = startText + middleText + endText;
	setSelection(textId, new UniRange(startText.length, startText.length + middleText.length));
}

function makeSoloTag(textId, tag) {
	var myRange = getSelection( textId);
	var ctrl = document.getElementById(textId);
	var text = ctrl.value;
	var startText = text.substring(0, myRange.start);
	var middleText = tag;
	var endText = text.substring(myRange.end);
	ctrl.value = startText + middleText + endText;
	setSelection(textId, new UniRange(startText.length + middleText.length, startText.length + middleText.length));
}

function makeAttachmentTag(textId, attach_id) {
	makeSoloTag( textId, '[attachment="' + attach_id + '"]');
}

function drawPalette( field_name, width, height){
	
	//set colors
	var colors = Array(
		"000000", "A0522D", "556B2F", "483D8B", "483D8B", "000080", "4B0082", "2F4F4F",
		"8B0000", "FF8C00", "808000", "008080", "008080", "0000FF", "708090", "696969",
		"FF0000", "FF8C00", "9ACD32", "2E8B57", "48D1CC", "4169E1", "800080", "808080",
		"FF00FF", "FFA500", "FFFF00", "00FF00", "00FFFF", "00BFFF", "9932CC", "C0C0C0",
		"FFC0CB", "F5DEB3", "FFFACD", "98FB98", "AFEEEE", "ADD8E6", "DDA0DD", "FFFFFF"
	);
	
	//draw them
	for( color_id in colors){
		document.write( "<a href=\"javascript:makeDoubleTag( '" + field_name + "', 'color', '#" + colors[color_id] + "')\" class=\"editor_color_swatch\" style=\"background-color: #" +  colors[color_id] + ";\"></a>");
	}
	
}